﻿@using HP.HPTRIM.ServiceModel;
@using System.Linq;
@using Resources;
@using HP.HPTRIM.Service;
@using HP.HPTRIM.Web.Configuration;
@inherits SecureViewPage
@{
	
    Dictionary<string, string> menuItems = new Dictionary<string, string>();

    string[] txtExtensions = new string[] { };


    if (!string.IsNullOrEmpty(ServiceDefaults.Instance.TextPage.Extensions))
    {
        txtExtensions = ServiceDefaults.Instance.TextPage.Extensions.ToLower().Split(new string[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
    }

    bool isDetailsView = this.Request.AbsoluteUri.IndexOf("/Record/", StringComparison.InvariantCultureIgnoreCase) > -1;
    
	if (Model.TrimObject.TrimType == BaseObjectTypes.Record)
    {
        if (Model.TrimObject.GetPropertyOrFieldValue("RecordIsElectronic") == true) {

            
            
            string extension = Model.TrimObject.GetPropertyOrFieldValue("RecordExtension");
            if (isDetailsView)
            {
                if (txtExtensions.Contains(extension.ToLower())) {                    
                    menuItems.Add(Translations.lang.preview, "RecordText/" + Model.TrimObject.Uri);
                }
                else { 
                    menuItems.Add(Translations.lang.preview, "RecordHtml/" + Model.TrimObject.Uri);
                }
            }
            else
            {
                if (txtExtensions.Contains(extension.ToLower()))
                {        
                    menuItems.Add(Translations.lang.home, "");
                }
                menuItems.Add(Translations.lang.details, "Record/" + Model.TrimObject.Uri);
            }

            menuItems.Add(Translations.lang.document, string.Format("Record/{0}/File/document",  Model.TrimObject.Uri));
        }


		if ((Model.TrimObject as Record).IsInPartSeries == true)
		{
			menuItems.Add(Translations.lang.menu_all_parts, string.Format("Record?q=allParts:{0}", Model.TrimObject.Uri));
		}

		if (!string.IsNullOrEmpty((Model.TrimObject as Record).RelatedRecs))
		{
			menuItems.Add(Translations.lang.menu_related_records, string.Format("Record?q=allRelated:{0}", Model.TrimObject.Uri));
		}
		if ((Model.TrimObject as Record).RecordType.UsualBehaviour == RecordBehaviour.Series)
		{
			menuItems.Add(Translations.lang.menu_series_contents, string.Format("Record?q=inSeries:{0}",  Model.TrimObject.Uri));
		}

		if ((Model.TrimObject as Record).PrimaryContact.Uri > 0)
		{
			menuItems.Add(Translations.lang.menu_same_contact, string.Format("Record?q=contact:{0}", (Model.TrimObject as Record).PrimaryContact.Uri));
		}

		if ((Model.TrimObject as Record).IsContainer)
		{
			menuItems.Add(Translations.lang.menu_contained_records, string.Format("Record?q=containerEx:{0}",  Model.TrimObject.Uri));
		}        
        
    }

	if (Model.TrimObject.TrimType == BaseObjectTypes.Record && this.Request.HttpMethod == "POST")
	{
		menuItems.Add(Translations.lang.details, string.Format("../{0}", @Model.TrimObject.Uri));
	}

	if (Model.TrimObject.TrimType == BaseObjectTypes.Record || Model.TrimObject.TrimType == BaseObjectTypes.Request)
	{
		menuItems.Add(Translations.lang.requests, string.Format("Request?q=record:{0}&sortby=completedOn-", (Model.TrimObject.TrimType == BaseObjectTypes.Record ? @Model.TrimObject.Uri : @Model.TrimObject.Record.Uri)));
	}
	if (!string.IsNullOrEmpty(Model.TrimObject.GetPropertyOrFieldValue("RecordCurrentAction")) && (Model.TrimObject as Record).EnabledCommandIds.Contains(CommandIds.RecCompleteCurrAct))
	{
		menuItems.Add("CompleteActions", "");
	}

	if (Model.TrimObject is Record && (Model.TrimObject as Record).EnabledCommandIds.Contains(CommandIds.RecAddRetrieveTemporaryRequest))
	{
		menuItems.Add("MakeRequest", "");
	}

    if (Model.TrimObject is Record && Model.TrimObject.GetPropertyOrFieldValue("RecordIsElectronic") == true)
    {
        menuItems.Add("Sign", "");
    }

	if (Model.TrimObject.TrimType == BaseObjectTypes.Request)
	{
        menuItems.Add(Translations.lang.menu_record, "Record/" + @Model.TrimObject.Record.Uri);
    }

	if (Model.TrimObject.TrimType == BaseObjectTypes.Location)
    {
        menuItems.Add(Translations.lang.menu_delegate_of, "Location?q=locDelegateOf:" + @Model.TrimObject.Uri);
        menuItems.Add(Translations.lang.menu_delegates,  "Location?q=locDelegateTo:" + @Model.TrimObject.Uri);

        if ((Model.TrimObject as Location).TypeOfLocation == LocationType.Person) {
            menuItems.Add(Translations.lang.menu_subordinates, "Location?q=locSupervisedBy:" + @Model.TrimObject.Uri);
        }
        
        if (!string.IsNullOrEmpty((Model.TrimObject as Location).AllMembers)) {
            menuItems.Add(Translations.lang.menu_members,  "Location?q=locMembers:" + @Model.TrimObject.Uri);
        }
        if (!string.IsNullOrEmpty((Model.TrimObject as Location).AllMemberships)) {
            menuItems.Add(Translations.lang.menu_member_of, "Location?q=locHasMember:" + @Model.TrimObject.Uri);
        }
    }

	if (Model.TrimObject.TrimType == BaseObjectTypes.Classification)
    {       
        menuItems.Add(Translations.lang.menu_subordinate_classifications, string.Format("Classification?q=parent:{0}~",  @Model.TrimObject.Uri));
    }

	if (Model.TrimObject.TrimType == BaseObjectTypes.Space && !(Model.TrimObject as Space).IsLowestLevel)
    {
        menuItems.Add(Translations.lang.menu_subordinate_spaces, string.Format("Space?q=within:{0}", @Model.TrimObject.Uri));
    }

	if (Model.TrimObject.TrimType == BaseObjectTypes.Keyword && (Model.TrimObject as Keyword).HasRelatedKeywords)
    {      
        
        menuItems.Add(Translations.lang.menu_narrower_terms, string.Format("Keyword?q=narrower:{0}", @Model.TrimObject.Uri));
        if (!(Model.TrimObject as Keyword).IsAPrimaryTerm) {
            menuItems.Add(Translations.lang.menu_broader_terms, string.Format("Keyword?q=broader:{0}", @Model.TrimObject.Uri));
        }

        menuItems.Add(Translations.lang.menu_use_for_terms, string.Format("Keyword?q=usedFor:{0}", @Model.TrimObject.Uri));
        menuItems.Add(Translations.lang.menu_related_terms, string.Format("Keyword?q=related:{0}", @Model.TrimObject.Uri));
        
    }
    
    
    <div class="banner">
        <div>
            @if (menuItems.Count() > 0) {
              <div class="dropdown pull-right">
                  <a class="btn dropdown-toggle" href="#" data-toggle="dropdown">
                    @Translations.lang.links_actions
                    <span class="caret"></span>
                  </a>
                  <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
                    @foreach(var menuItem in menuItems)
                    {
                        <li>
                            @if (menuItem.Key == "CompleteActions")
                            {
							
								  <form 
                                    class="menu-form" 
                                    method="post" 
                                    action="~/CompleteActions">
									  <input name="Uri" type="hidden" value="@Model.TrimObject.Uri" />
									  <input name="ServiceAction" type="hidden" value="CompleteActions" />
                                    <button class="btn btn-small" type="submit">@Translations.lang.complete_action</button>
                                </form>
                            }
                            else if (menuItem.Key == "MakeRequest")
                            {                      

								<form class="menu-form" method="post" action="~/Request">                                  
                              
                                    <input type="hidden" name="RequestRecord" value="@Model.TrimObject.Uri" />
                                    <button class="btn btn-small" type="submit">@Translations.lang.make_request</button>
                                </form>
                            }
                            else if (menuItem.Key == "Sign")
                            {
                                <form class="menu-form" method="get" action="~/DocuSign">

                                    <input type="hidden" name="Uri" value="@Model.TrimObject.Uri" />
                                    <button class="btn btn-small" type="submit">Sign</button>
                                </form>
                            }
                            else
                            {
                                <a tabindex="-1" href="~/@menuItem.Value">@menuItem.Key</a>
                            }
                        </li>

                    }
                  </ul>
              </div>
            }
        </div>

		<h2>@ViewBag.Title</h2>

	</div>


}

